﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Drawing;
using System.Drawing.Imaging;

namespace SCG.TurboSprite.TileBased
{
    public class MapDecoration
    {     
        //constructors
        public MapDecoration()
        {
        }
        public MapDecoration(MapSet mapSet)
        {
            _mapSet = mapSet;
        }

        //The image file name, must be in same folder as mapset
        public string ImageFileName { get; set; }

        //The locattion of the decoration
        public int X { get; set; }
        public int Y { get; set; }

        //Alpha value
        public float Alpha
        {
            get
            {
                return _alpha;
            }
            set
            {
                if (value < 0)
                    value = 0;
                if (value > 1.0f)
                    value = 1.0f;
                _alpha = value;
            }
        }

        //Get an ImageAttributes based on alpha
        [XmlIgnore]
        public ImageAttributes ImageAttributes
        {
            get
            {
                if (_alpha == 1.0f)
                    return null;
                if (_ia != null)
                    return _ia;
                ColorMatrix cm = new ColorMatrix();
                cm.Matrix33 = _alpha;
                _ia = new ImageAttributes();
                _ia.SetColorMatrix(cm);
                return _ia;
            }
        }

        //Access the MapSet
        [XmlIgnore]
        public MapSet MapSet
        {
            get
            {
                return _mapSet;
            }
            set
            {
                _mapSet = value;
            }
        }

        //Get the Image (with alpha)
        [XmlIgnore]
        public Bitmap Bitmap
        {
            get
            {
                if (_bmp != null)
                    return _bmp;
                _bmp = (Bitmap)Bitmap.FromFile(MapSet.ImagePath + @"\" + ImageFileName);
                _bmp.MakeTransparent(Color.White);
                return _bmp;
            }
        }

        //private members
        private Bitmap _bmp = null;
        private MapSet _mapSet;
        private float _alpha = 0.3f;
        private ImageAttributes _ia = null;
    }
}
